package com.tekj.cxqcb.view.Module.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectByLevelBean;
import com.tekj.cxqcb.view.Module.adapter.GoodsLevelAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;

/**
 * 安装地址Activity
 */

public class GoodsLevelActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.rv_address)
    RecyclerView rvAddress;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private List<SelectByLevelBean.DataBean.ListBean> mData;
    private GoodsLevelAdapter adapter;
    private BusinessDao mod4Dao;

    private String id, name;

    @Override
    public void init() {
        tvTitle.setText("分类选择");
        mod4Dao = new BusinessDao(activity, this);

        rvAddress.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvAddress.setHasFixedSize(true);
        adapter = new GoodsLevelAdapter(null);

        rvAddress.setAdapter(adapter);
        mod4Dao.selectByLevel(1, "");

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mData.get(position).getLevel() == 1) {
                    id = mData.get(position).getId();
                    name = mData.get(position).getTypeName();
                    mod4Dao.selectByLevel(2, id);
                } else if (mData.get(position).getLevel() == 2) {
                    Intent intent = new Intent();
                    intent.putExtra("id", id);
                    intent.putExtra("id2", mData.get(position).getId());
                    intent.putExtra("name", name);
                    intent.putExtra("name2", mData.get(position).getTypeName());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    @Override
    public int setLayout() {
        return R.layout.activity_address2;
    }


    @OnClick({R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;

        }
    }


    boolean isfirst = true;

    @Override
    public void initLoadStart() {
        if (isfirst) {
            isfirst = !isfirst;
            loading.show();
        }

    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bean = (BindingViewBean) mBean;
        switch (bean.getmEnumStatus()) {
            case 店铺服务分类:
                SelectByLevelBean data = (SelectByLevelBean) bean.getBean();
                if (data.getCode().equals("0000000")) {
                    mData = data.getData().getList();
                    adapter.setNewData(data.getData().getList());
                    adapter.loadMoreEnd();
                }

                break;
            case 店铺服务分类2:
                SelectByLevelBean data2 = (SelectByLevelBean) bean.getBean();
                if (data2.getCode().equals("0000000")) {
                    mData = data2.getData().getList();
                    adapter.setNewData(data2.getData().getList());
                    adapter.loadMoreEnd();
                }

                break;
        }

    }

}
